package com;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;

public class ChessPanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	public final static int OPRATION_START=0XE0;
	public final static int OPRATION_ACCEPT_START=0XE1;
	public final static int OPRATION_GIVEUP=0XE2;
	public final static int OPRATION_ACCEPT_GIVEUP=0XE3;
	public final static int OPRATION_REFUSE_GIVEUP=0XE4;
	static ImageIcon WHITE_CHESS;
	static ImageIcon BLACK_CHESS;
	private Image bkimg;
	private GridPanel gridpanel;
	private JButton backButton = new JButton();
	private JButton heqiButton = new JButton();
	private JButton startButton = new JButton();
	private JButton giveupButton = new JButton();
	private JLabel myChessColorLabel;
	private JLabel towardsChessColorLabel;
	JLabel leftInfoLabel;
	JLabel rightInfoLabel;
	
	public ChessPanel() {
		WHITE_CHESS=new ImageIcon(getClass().getResource("/res/whiteChess.png"));
		BLACK_CHESS=new ImageIcon(getClass().getResource("/res/blackChess.png"));
		URL url = getClass().getResource("/res/bg/3.jpg");
		bkimg = new ImageIcon(url).getImage();
		initCompnents();
	}
	
	public GridPanel getGridPanel(){
		return gridpanel;
	}
	
	public void setCurrentPlayer(boolean isMy){
		if(isMy){
			myChessColorLabel.setVisible(true);
			towardsChessColorLabel.setVisible(false);
		}
		else{
			myChessColorLabel.setVisible(false);
			towardsChessColorLabel.setVisible(true);
		}
	}
	
	public void reInit(){
		startButton.setEnabled(true);
		heqiButton.setEnabled(false);
		giveupButton.setEnabled(false);
		backButton.setEnabled(false);
		gridpanel.setStart(false);
		gridpanel.setMy(false);
		gridpanel.setOpponentStart(false);
	}
	private void initCompnents() {
		setLayout(new BorderLayout());
		gridpanel=new GridPanel();
		add(gridpanel,BorderLayout.CENTER);
		JPanel jPanel1 = new JPanel();
		jPanel1.setOpaque(false);
		JToggleButton backplayToggleButton = new JToggleButton();
		JPanel jPanel2 = new JPanel();
		jPanel2.setOpaque(false);
		JLabel jLabel5 = new JLabel();
		leftInfoLabel = new JLabel();
		leftInfoLabel.setForeground(new Color(0, 255, 0));
		leftInfoLabel.setFont(new Font("隶书", Font.PLAIN, 22));
		myChessColorLabel = new JLabel();
		JPanel jPanel3 = new JPanel();
		jPanel3.setOpaque(false);
		JLabel jLabel6 = new JLabel();
		rightInfoLabel = new JLabel();
		rightInfoLabel.setForeground(Color.GREEN);
		rightInfoLabel.setFont(new Font("隶书", Font.PLAIN, 22));
		towardsChessColorLabel = new JLabel();
		JPanel jPanel4 = new JPanel();
		jPanel4.setOpaque(false);
		JLabel bannerLabel = new JLabel();
		backButton.setText("悔棋");
		backButton.setEnabled(false);
		jPanel1.add(backButton);
		heqiButton.setText("和棋");
		heqiButton.setEnabled(false);
		jPanel1.add(heqiButton);
		startButton.setText("开始");
		startButton.addActionListener(this);
		jPanel1.add(startButton);
		giveupButton.setText("认输");
		giveupButton.addActionListener(this);
		giveupButton.setEnabled(false);
		jPanel1.add(giveupButton);
		backplayToggleButton.setText("游戏回放");
		jPanel1.add(backplayToggleButton);
		add(jPanel1, java.awt.BorderLayout.PAGE_END);
		jPanel2.setPreferredSize(new Dimension(110, 100));
		jPanel2.setLayout(new FlowLayout(java.awt.FlowLayout.CENTER,50, 60));
		jLabel5.setPreferredSize(new Dimension(42, 55));
		jPanel2.add(jLabel5);
		leftInfoLabel.setIcon(new ImageIcon(getClass().getResource("/res/infoPanelLeft.png")));
		leftInfoLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		leftInfoLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
		jPanel2.add(leftInfoLabel);
		jPanel2.add(myChessColorLabel);
		add(jPanel2, BorderLayout.LINE_START);
		jPanel3.setPreferredSize(new Dimension(110, 100));
		jPanel3.setLayout(new FlowLayout(FlowLayout.CENTER,50, 60));
		jLabel6.setPreferredSize(new Dimension(42, 55));
		jPanel3.add(jLabel6);
		rightInfoLabel.setIcon(new ImageIcon(getClass().getResource("/res/infoPanel.png")));
		rightInfoLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		rightInfoLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
		jPanel3.add(rightInfoLabel);
		jPanel3.add(towardsChessColorLabel);
		add(jPanel3, BorderLayout.LINE_END);
		jPanel4.setLayout(new BorderLayout());
		bannerLabel.setHorizontalAlignment(SwingConstants.CENTER);
		bannerLabel.setIcon(new ImageIcon(getClass().getResource("/res/logo.png"))); // NOI18N
		bannerLabel.setBorder(BorderFactory.createEmptyBorder(1, 1,5, 1));
		bannerLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jPanel4.add(bannerLabel, BorderLayout.CENTER);
		add(jPanel4, java.awt.BorderLayout.PAGE_START);
		GroupLayout gl=new GroupLayout(gridpanel);
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGap(0, 280, Short.MAX_VALUE));
		gl.setVerticalGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGap(0, 248, Short.MAX_VALUE));
		gridpanel.setLayout(gl);
	}
	@Override
	protected void paintComponent(Graphics g) {
		g.drawImage(bkimg,0,0, getWidth(), getHeight(), null);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==startButton){
			MainFrame mf=MainFrame.getInstence();
			if(mf.getSocket()==null) return;
			gridpanel.setStart(true);
			mf.getChessColor();
			if(gridpanel.getChessColor()==Color.white){
				myChessColorLabel.setIcon(WHITE_CHESS);
				towardsChessColorLabel.setIcon(BLACK_CHESS);
			}
			else{
				myChessColorLabel.setIcon(BLACK_CHESS);
				towardsChessColorLabel.setIcon(WHITE_CHESS);
			}
			mf.send(mf.getSocket(), OPRATION_START);
			startButton.setEnabled(false);
			heqiButton.setEnabled(true);
			giveupButton.setEnabled(true);
			ChessArray.getInstance().clear();
			ChessArray.getInstance().chessStack.clear();
			repaint();
		}
		else if(e.getSource()==giveupButton){
			MainFrame mf=MainFrame.getInstence();
			if(mf.getSocket()==null) return;
			mf.send(mf.getSocket(), OPRATION_GIVEUP);
		}
	}
}
